package com.aiatomic.couting.account;

import java.text.SimpleDateFormat;

import com.aiatomic.couting.AccountInfoActivity;
import com.aiatomic.couting.R;
import com.aiatomic.couting.models.OrderEntity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FragmentAccountInfoOrdersDetails extends Fragment {
	private View view;
	private AccountInfoActivity activity;
	private OrderEntity order;
	private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public FragmentAccountInfoOrdersDetails(OrderEntity order) {
		super();
		this.order = order;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_account_info_orders_details, container, false);
		activity = (AccountInfoActivity) getActivity();
		((TextView)view.findViewById(R.id.buyed_id_val)).setText(order.getGm_order_id());
		((TextView)view.findViewById(R.id.buyed_good_name_val)).setText(order.getGood_name());
		((TextView)view.findViewById(R.id.buyed_currency_val)).setText(String.valueOf(order.getReal_give_days()));
		((TextView)view.findViewById(R.id.buyed_money_val)).setText(String.valueOf(order.getReal_money()));
		((TextView)view.findViewById(R.id.buyed_time_val)).setText(dateformat.format(order.getCreated_time_loc()));
		Button buyAgain=((Button) view.findViewById(R.id.orders_details_buy_again));
		buyAgain.setVisibility(View.INVISIBLE);
		switch (order.getState()) {
		case PAY_FIN:
			((TextView)view.findViewById(R.id.buyed_state_val)).setText(R.string.PAY_FIN);
			buyAgain.setVisibility(View.VISIBLE);

			break;
		case PAY_CLOSE:
			((TextView)view.findViewById(R.id.buyed_state_val)).setText(R.string.PAY_CLOSE);
			break;
		case PAY_SUCCESS:
			buyAgain.setVisibility(View.VISIBLE);
			((TextView) view.findViewById(R.id.buyed_state_val)).setText(R.string.PAY_SUCCESS);
			break;
		case PAY_WAIT:
			((TextView) view.findViewById(R.id.buyed_state_val)).setText(R.string.PAY_WAIT);
			break;
		case PAY_ADD:
			((TextView) view.findViewById(R.id.buyed_state_val)).setText(R.string.PAY_ADD);
			break;
		}

		bindEvent();
		return view;
	}

	private void bindEvent() {
		view.findViewById(R.id.orders_details_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				activity.onBackPressed();
			}
		});

		view.findViewById(R.id.orders_details_buy_again).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				activity.addOrder(order.getGood_id());
			}
		});
	}
}
